package com.cms.controllers.admin;

import com.cms.common.aop.permission.PermissionCheck;
import com.cms.common.model.vo.ResponseResult;
import com.cms.controllers.BaseController;
import com.cms.plugin.services.PluginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller()
@RequestMapping("/admin/plugins")
public class PluginsController extends BaseController {

    @Autowired
    private PluginService pluginService;

    @GetMapping("")
    @PermissionCheck(name = "插件配置", typeName = "插件管理")
    public String index(Model model) {
        return "admin/plugin/index";
    }

    @GetMapping("/list")
    @ResponseBody
    @PermissionCheck(name = "插件列表", typeName = "插件管理")
    public ResponseResult list() {
        return ResponseResult.success(pluginService.findAll());
    }

    @PostMapping("/enable/{pluginId}")
    @ResponseBody
    @PermissionCheck(name = "启用插件", typeName = "插件管理")
    public ResponseResult enable(@PathVariable String pluginId) {
        try {
            pluginService.enable(pluginId);
            return ResponseResult.success("启用成功");
        } catch (Exception e) {
            return ResponseResult.fail("启用失败：" + e.getMessage());
        }
    }

    @PostMapping("/disable/{pluginId}")
    @ResponseBody
    @PermissionCheck(name = "禁用插件", typeName = "插件管理")
    public ResponseResult disable(@PathVariable String pluginId) {
        try {
            pluginService.disable(pluginId);
            return ResponseResult.success("禁用成功");
        } catch (Exception e) {
            return ResponseResult.fail("禁用失败：" + e.getMessage());
        }
    }

    @PostMapping("/config/{id}")
    @ResponseBody
    @PermissionCheck(name = "配置插件", typeName = "插件管理")
    public ResponseResult updateConfig(@PathVariable Long id, @RequestBody Map<String, Object> config) {
        try {
            pluginService.updateConfig(id, config);
            return ResponseResult.success("配置更新成功");
        } catch (Exception e) {
            return ResponseResult.fail("配置更新失败：" + e.getMessage());
        }
    }
} 